﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FONCREAGROINTEGRAL.Models;

namespace FONCREAGROINTEGRAL.Controllers
{
    public class ctscontableController : Controller
    {
        public SistIntFoncreagroEntities entidades = new SistIntFoncreagroEntities();
        //
        // GET: /ctscontable/

        public ActionResult Index(int id)
        {
            var query = entidades.CTSCONTABLEs.Where(cc => cc.IdCTServ == id);

            var result = (from c in query.OrderBy(c => c.IdProyecto).ToList()
                          join p in entidades.PROYECTOes on c.IdProyecto equals p.IdProyecto
                          join cc in entidades.CCOSTOes on c.IdCCosto equals cc.IdCCosto
                          select new
                          {
                              IdCTSContable = c.IdCTSContable,
                              Monto = c.Monto,
                              IdProyecto = c.IdProyecto,
                              Proyecto = p.Codigo,
                              IdCCosto = c.IdCCosto,
                              CCosto = cc.Codigo,
                              FechaPago = string.Format("{0:d}", c.FechaPago)
                          }).ToList();
            return Json(new {grid = result,total = query.Count() },JsonRequestBehavior.AllowGet);
        }

        //
        // GET: /ctscontable/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ctscontable/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ctscontable/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /ctscontable/Edit/5
 
        //public ActionResult Edit(int id)
        //{
        //    return View();
        //}

        //
        // POST: /ctscontable/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            string strResponse = String.Empty;
            try
            {
                // TODO: Add update logic here
                decimal Monto = Convert.ToDecimal(Request.Form["Monto"]);
                var dr = entidades.CTSCONTABLEs.Single(c => c.IdCTSContable == id);
                dr.Monto = Monto;
                entidades.SaveChanges();
                strResponse = "{success: true,msg:'Registro Creado Correctamente'}";
                return Content(strResponse);
            }
            catch (Exception e)
            {
                string sms = e.Message;
                strResponse = "{success: false,msg:\"" + sms + "\"}";
                return Content(strResponse);
            }
        }

        //
        // GET: /ctscontable/Delete/5
 
        //public ActionResult Delete(int id)
        //{
        //    return View();
        //}

        //
        // POST: /ctscontable/Delete/5

        //[HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var dr = entidades.CTSCONTABLEs.Single(c => c.IdCTSContable == id);
                entidades.DeleteObject(dr);
                entidades.SaveChanges();
                return Content("1");
            }
            catch
            {
                return Content("2");
            }
        }
    }
}
